$(document).ready(function() {
	moment.locale("zh-cn");
	window.currentpage = 1;
	ready();
	
});

function ready() {
	queryUserInfo(1);//查询第一页
	//绑定事件
	bind();
	//表单校验
	check();
	showUserName();
}

function initDormLocation() {
	$.ajax({
		url:"/dormms/super/getAllProclamationInfo",
        type:"post",
        success:function(data){
        	console.log(data);
        	for(var i=0; i<data.length; i++){
        		$("#insertProclamationId").append("<option value="+ data[i].proclamationId+">"+data[i].proclamationTitle+"</option>");
        	}
        }
	});
}

function bind() {
	$("#pageSize").bind("change",function(){
		queryUserInfo(window.currentpage);
	});
	$("#searchProclamationByProclamationTitle").bind("click",function(){
		window.currentpage = 1;
		queryUserInfo(window.currentpage);
	});
	$('#addProclamationModal').on('hide.bs.modal', function (){
		$("#insertProclamationTitle").val("");
		$("#insertProclamationDescr").val("");
		$("#insertProclamationTitle-error").remove();
		$("#insertProclamationDescr-error").remove();
	});
	$('#addProclamationRoleModal').on('show.bs.modal', function (){
		initDormLocation();
	});
	$('#addProclamationRoleModal').on('hide.bs.modal', function (){
		$("#insertProclamationId").val("");
		$("#insertRoleId").val("");
		$("#insertProclamationId-error").remove();
		$("#insertRoleId-error").remove();
		$("#insertProclamationId option").remove();
	});
	$('#updateProclamationModal').on('hide.bs.modal', function (){
		$("#updateProclamationId").val("");
		$("#updateProclamationTitle").val("");
		$("#updateProclamationId-error").remove();
		$("#updateProclamationTitle-error").remove();
		$("#updateProclamationDescr").val("");
		$("#updateProclamationDescr-error").remove();
	});
	$('#updatePasswordModel').on('hide.bs.modal', function (){
		$("#oldPassword").val("");
		$("#password").val("");
		$("#password_again").val("");
		$("#oldPassword-error").remove();
		$("#password-error").remove();
		$("#password_again-error").remove();
	});
	$('input[name="selectAllDormAdmin"]').bind("click",function(){
		if($(this).is(':checked')){
			$('input[name="selectDormAdmin"]').each(function(){   
                $(this).prop("checked",true);  
            });
		}else{
			$('input[name="selectDormAdmin"]').each(function(){   
                $(this).removeAttr("checked",false);
            });
		}
	});
}

function check(){
	addCheck();
	addAdminCheck();
	updateCheck();
	updatePasswordCheck();
}


function addCheck(){
	//添加校验
	$("#addProclamation").validate({
		rules:{
			insertProclamationTitle:{
				required:true,
			},
			insertProclamationDescr:{
				required:true,
			}
		},
		messages:{
			insertProclamationTitle:{
				required:"请输入标题",
			},
			insertProclamationDescr:{
				required:"请输入内容",
			}
		},
		submitHandler:function(form){
			var insertProclamationTitle = $("#insertProclamationTitle").val();
			var insertProclamationDescr = $("#insertProclamationDescr").val();
			$.ajax({
				type:'post',
				url:'insertProclamation',
				data:{"proclamationTitle":insertProclamationTitle,"proclamationDescr":insertProclamationDescr},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#addProclamationModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function addAdminCheck(){
	$("#addProclamationRole").validate({
		rules:{
			insertProclamationId:{
				required:true,
			},
			insertRoleId:{
				required:true,
			}
		},
		messages:{
			insertProclamationId:{
				required:"请选择公告",
			},
			insertRoleId:{
				required:"请选择角色",
			}
		},
		submitHandler:function(form){
			var insertRoleId = $("#insertRoleId").val();
			var insertProclamationId = $("#insertProclamationId").val();
			$.ajax({
				type:'post',
				url:'insertProclamationInfoRole',
				data:{"roleId":insertRoleId,"proclamationId":insertProclamationId},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#addProclamationRoleModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updateCheck(){
	//修改校验
	$("#updateProclamation").validate({
		rules:{
			updateProclamationId:{
				required:true,
			},
			updateProclamationTitle:{
				required:true,
			},
			updateProclamationDescr:{
				required:true,
			},
		},
		messages:{
			updateProclamationId:{
				required:"请输入编号",
			},
			updateProclamationTitle:{
				required:"请输入标题",
			},
			updateProclamationDescr:{
				required:"请输入内容",
			}
		},
		submitHandler:function(form){
			var updateProclamationId = $("#updateProclamationId").val();
			var updateProclamationTitle = $("#updateProclamationTitle").val();
			var updateProclamationDescr = $("#updateProclamationDescr").val();
			$.ajax({
				type:'post',
				url:'updateProclamationBySuper',
				data:{"proclamationId":updateProclamationId,"proclamationTitle":updateProclamationTitle,"proclamationDescr":updateProclamationDescr},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updateProclamationModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updatePasswordCheck(){
	$("#updatePassword").validate({
		rules:{
			oldPassword:{
				required:true
			},
			password:{
				required:true
			},
			password_again:{
				required:true,
				equalTo: "#password"
			}
		},
		messages:{
			oldPassword:{
				required:"请输入旧密码"
			},
			password:{
				required:"请输入新密码"
			},
			password_again:{
				required:"请输入新密码",
				equalTo: "两次输入不相同"
			}
		},
		submitHandler:function(form){
			var oldPassword = $("#oldPassword").val();
			var password = $("#password").val();
			$.ajax({
				type:'post',
				url:'updatePassword',
				data:{"oldPassword":oldPassword,"password":password},
				success:function(data){
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updatePasswordModel').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function queryUserInfo(page){
	var currPage = page;
	var pageSize = $("#pageSize").val();
	var proclamationTitle = $("#proclamationTitle").val();
	$.ajax({
		url:"/dormms/super/queryProclamationByPageBySuper",
        type:"post",
        data:{"currPage":currPage,"pageSize":pageSize,"proclamationTitle":proclamationTitle},
        success:function(data){
        	console.log(data);
        	$("#proclamationInfoList").empty();
        	$("#total").text("共有"+data.total+"条记录");
        	for(var i=0; i<data.list.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td>"+ data.list[i].proclamationId +"</td>");
        		 trTemp.append("<td>"+ data.list[i].proclamationTitle +"</td>");
        		 trTemp.append("<td>"+ moment(data.list[i].proclamationCreate).format('L') +"</td>");
        		 trTemp.append("<td>" + "<button type='button' class='btn btn-outline-success btn-sm' onclick='getDormAdminInfoGet(this)'>查看</button>" 
        				 +"<button type='button' class='btn btn-outline-primary btn-sm' onclick='getDormAdminInfoUpdate(this)'>修改</button>" 
        				 + "<button type='button' class='btn btn-outline-danger btn-sm' onclick='deleteProclamation(this)'>删除</button>" 
        				 + "</td>");
        		 trTemp.appendTo("#proclamationInfoList");
        	}
        	queryUserRoleByPage(data);
        }
	});
}

function queryUserRoleByPage(data) {
	var options = {
			bootstrapMajorVersion: 3,
			currentPage: data.pageNum,
            totalPages: data.pages,
            numberofPages: 5,
            itemTexts: function(type, page, current) { //修改显示文字
                switch (type) {
                case "first":
                    return "&laquo;";
                case "prev":
                    return "&lt;";
                case "next":
                    return "&gt;";
                case "last":
                    return "&raquo;";
                case "page":
                    return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) { //异步换页
            	queryUserInfo(page);
            	window.currentpage=page;
            },
	};
	$('#page').bootstrapPaginator(options); 
}


//获取当前行数据(修改)
function getDormAdminInfoUpdate(obj) {
	var x = $(obj).parent().parent().find("td");
	var dormAdmininfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/getProclamationAllRole",
		type:"get",
		data:{'proclamationId':dormAdmininfo},
		success:function(data){
			console.log(data);
			$("#updateProclamationId").val(data.proclamationInfo.proclamationId);
			$("#updateProclamationTitle").val(data.proclamationInfo.proclamationTitle);
			$("#updateProclamationDescr").val(data.proclamationInfo.proclamationDescr);
			$("#updateRoleList").empty();
			var role =["管理员","宿舍管理员","学生","维修人员"];
        	for(var i=0; i<data.roles.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td>"+ data.roles[i].roleId +"</td>");
        		 trTemp.append("<td>"+ role[data.roles[i].roleId-1] +"</td>");
        		 trTemp.append("<td><button type='button' class='btn btn-outline-danger btn-sm' onclick='deleteProclamationRole(this)'>删除</button></td>");
        		 trTemp.appendTo("#updateRoleList");
        	}
			$('#updateProclamationModal').modal('show');
		}
	});
}
//获取当前行数据(查看)
function getDormAdminInfoGet(obj) {
	var x = $(obj).parent().parent().find("td");
	var dormAdmininfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/getProclamationAllRole",
		type:"get",
		data:{'proclamationId':dormAdmininfo},
		success:function(data){
			console.log(data);
			$("#getProclamationId").val(data.proclamationInfo.proclamationId);
			$("#getProclamationTitle").val(data.proclamationInfo.proclamationTitle);
			$("#getProclamationDescr").val(data.proclamationInfo.proclamationDescr);
			$("#getProclamationList").empty();
			var role =["管理员","宿舍管理员","学生","维修人员"];
        	for(var i=0; i<data.roles.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td>"+ data.roles[i].roleId +"</td>");
        		 trTemp.append("<td>"+ role[data.roles[i].roleId-1] +"</td>");
        		 trTemp.appendTo("#getProclamationList");
        	}
			$('#getProclamationModal').modal('show');
		}
	});
}

function deleteProclamation(obj){
	var x = $(obj).parent().parent().find("td");
	var proclamationId = x.eq(0).text();
	$.ajax({
		url:"deleteProclamation",
		type:"get",
		data:{'proclamationId':proclamationId},
		success:function(data){
			queryUserInfo(window.currentpage);
		}
	});
}

function deleteProclamationRole(obj){
	var x = $(obj).parent().parent().find("td");
	var roleId = x.eq(0).text();
	var proclamationId = $("#updateProclamationId").val();
	$.ajax({
		url:"deleteProclamationRole",
		type:"get",
		data:{'roleId':roleId,'proclamationId':proclamationId},
		success:function(data){
			var proclamationId = $("#updateProclamationId").val();
			$.ajax({
				url:"getProclamationAllRole",
				type:"get",
				data:{'proclamationId':proclamationId},
				success:function(data){
					console.log(data);
					$("#updateProclamationId").val(data.proclamationInfo.proclamationId);
					$("#updateProclamationTitle").val(data.proclamationInfo.proclamationTitle);
					$("#updateProclamationDescr").val(data.proclamationInfo.proclamationDescr);
					$("#updateRoleList").empty();
					var role =["管理员","宿舍管理员","学生","维修人员"];
		        	for(var i=0; i<data.roles.length; i++){
		        		 var trTemp = $("<tr></tr>");
		        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
		        		 trTemp.append("<td>"+ data.roles[i].roleId +"</td>");
		        		 trTemp.append("<td>"+ role[data.roles[i].roleId-1] +"</td>");
		        		 trTemp.append("<td><button type='button' class='btn btn-outline-danger btn-sm' onclick='deleteProclamationRole(this)'>删除</button></td>");
		        		 trTemp.appendTo("#updateRoleList");
		        	}
					$('#updateProclamationModal').modal('show');
				}
			});
		}
	});
}

function showPasswordModel() {
	$('#updatePasswordModel').modal('show');
}

function showUserName() {
	$.ajax({
		url:"getSuperInfo",
		type:"get",
		success:function(data){
			$("#userId").text(data.userId);
		}
	});
}
